using UnityEngine.SceneManagement;
using UnityEngine;
using ControllerApplication;
using InterfaceApplication;
using CheckApplication;

public class FirstController : MonoBehaviour,ISceneController,IUserAction{
    public LandModel startLand;
    public LandModel endLand;
    public Water water;
    public BoatModel boat;
    public RoleModel[] roles;
    public UserGUI GUI;
    public Check gameStatusManager;
    public ActionManager actionManager;

    void Start(){
        SSDirect director=SSDirect.getInstance();
        director.CurrentSceneController=this;
        GUI=gameObject.AddComponent<UserGUI>() as UserGUI;
        actionManager=gameObject.AddComponent<ActionManager>() as ActionManager;
        gameStatusManager=gameObject.AddComponent<Check>() as Check;
        LoadResoureces();
    }

    public void LoadResoureces(){
        water=new Water();//构建水
        startLand=new LandModel("start");//构建两岸
        endLand=new LandModel("end");
        boat=new BoatModel();//构建船
        roles=new RoleModel[6];//构建角色
        for(int i=0;i<3;i++) {
            RoleModel role=new RoleModel("priest", startLand.getEmptyPos());
            role.setName("priest"+i);
            startLand.addRole(role);
            roles[i]=role;
        }
        for(int i=0;i<3;i++) {
            RoleModel role=new RoleModel("devil", startLand.getEmptyPos());
            role.setName("devil"+i);
            startLand.addRole(role);
            roles[i+3]=role;
        }
    }
    public void moveBoat(){
        if (boat.empty() || GUI.sign!=0) return;
        Vector3 endPos;
        boat.changeBoatSign();
        if(boat.getBoatSign()==-1) endPos=new Vector3(-5,0.5f,0);
        else endPos=new Vector3(5,0.5f,0);
        actionManager.moveBoat(boat.getBoat(),endPos,boat.boatSpeed);
        GUI.sign=gameStatusManager.CheckGame();
    }
    public void moveRole(RoleModel role){
        if(GUI.sign!=0) return;
        Vector3 middlePos,endPos;
        if(role.getOnBoat()==1) {
            boat.deleteRole(role.getName());
            role.setOnBoat(0);
            role.getRole().transform.parent=null;
            if(boat.getBoatSign()==1) {
                endPos=startLand.getEmptyPos();
                role.setLandSign(1);
                startLand.addRole(role);
            }
            else {
                endPos=endLand.getEmptyPos();
                role.setLandSign(-1);
                endLand.addRole(role);
            }
            middlePos=new Vector3(role.getRole().transform.position.x,endPos.y,endPos.z);
            actionManager.moveRole(role.getRole(),middlePos,endPos,role.roleSpeed);  
        }else{
            if(role.getLandSign()==1) {
                if(boat.getEmptyNumber()==-1 || role.getLandSign()!=boat.getBoatSign()) return;
                startLand.deleteRole(role.getName()); 
            }else{
                if(boat.getEmptyNumber() == -1 || role.getLandSign()!=boat.getBoatSign()) return;
                endLand.deleteRole(role.getName());
            }
            endPos=boat.getEmptyPos();
            middlePos=new Vector3(endPos.x,role.getRole().transform.position.y,endPos.z);
            actionManager.moveRole(role.getRole(),middlePos,endPos,role.roleSpeed);
            role.getRole().transform.parent=boat.getBoat().transform;
            role.setOnBoat(1);
            boat.addRole(role);
        }
        GUI.sign=gameStatusManager.CheckGame();
    }
    public void reStart(){
        SceneManager.LoadScene(0);
    }
}
